<template>
  <!-- 折线图(折点) -->
  <div style="width: 100%; height: 100%">
    <div ref="oneLeft" class="multiBeam" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'BrokenLine',
  props: {
    chartData: {
      type: Object
    },
    /** 颜色列表 */
    colorList: {
      type: Array
    },
    /** 盒子边距 */
    positionGrid: {
      type: Object,
      default: () => {
        return {
          top: '20%',
          left: '5%',
          bottom: '40',
          right: '0%'
        }
      }
    },
    /** 折线类别数组 */
    lenged: {
      type: Array,
      default: () => {
        return ['立法申请数量趋势', '立法引用率趋势']
      }
    },
    /** 折线类别数组显示 */
    lengedshow: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      // 柱状图配置项
      scoreOption: {}
    }
  },
  destroyed() {
    window.removeEventListener('resize', this.onResize)
    window.onresize = null
  },
  mounted() {
    this.draw()
  },
  methods: {
    draw() {
      console.log(11, this.chartData, this.lengthData)
      this.chart = echarts.init(this.$refs.oneLeft)
      this.scoreOption = this.options || {
        backgroundColor: '#fff',
        color: this.colorList,
        legend: {
          show: this.lengedshow,
          align: 'left',
          right: '0%',
          top: '0%',
          itemWidth: 16,
          itemHeight: 2,
          data: this.legend
        },
        // title: {
        //   text: '',
        //   textStyle: {
        //     fontSize: 12,
        //     fontWeight: 400
        //   },
        //   left: 'center',
        //   top: '5%'
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            label: {
              show: true,
              backgroundColor: '#fff',
              color: '#556677',
              borderColor: 'rgba(0,0,0,0)',
              shadowColor: 'rgba(0,0,0,0)',
              shadowOffsetY: 0
            },
            lineStyle: {
              width: 0
            }
          },
          backgroundColor: '#fff',
          textStyle: {
            color: '#5c6c7c'
          },
          padding: [10, 10],
          extraCssText: 'box-shadow: 1px 0 2px 0 rgba(163,163,163,0.5)'
        },
        grid: this.positionGrid,
        xAxis: [
          {
            type: 'category',
            data: this.chartData.xData,
            axisLine: {
              show: true,
              lineStyle: {
                color: 'rgb(153,153,153)'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0,
              textStyle: {
                color: '#666666',
                fontSize: 18
              },
              //   formatter(val) {
              //     var strs = val.split('') // 字符串数组
              //     var str = ''
              //     for (var i = 0, s; (s = strs[i++]); i++) {
              //       // 遍历字符串数组
              //       str += s
              //       if (!(i % 5)) str += '\n' // 按需要求余
              //     }
              //     return str
              //   },

              // 默认x轴字体大小
              //   fontSize: 12,
              //   left: '5%',
              // margin:文字到x轴的距离
              margin: 20
            },
            axisPointer: {
              label: {
                show: false
              }
            }
            // boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value',
            minInterval: 5,
            // name: '立法申请数量趋势',
            nameTextStyle: {
              color: '#666',
              fontSize: 14,
              padding: [20, 0, 0, 40]
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#DCE2E8'
              }
            },
            axisLabel: {
              formatter(value, index) {
                if (value >= 10000 && value < 10000000) {
                  value = value / 10000 + '万'
                } else if (value >= 10000000) {
                  value = value / 10000000 + '千万'
                }
                return value
              },
              textStyle: {
                color: '#666666',
                fontSize: 18
              }
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(153, 153, 153, .2)',
                type: 'dashed',
                width: 1
              }
            }
          },
          {
            show: false,
            // name: '立法引用率趋势',
            type: 'value',
            nameTextStyle: {
              color: '#666',
              fontSize: 14,
              padding: [20, 40, 0, 0]
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              formatter: '{value}%',
              textStyle: {
                color: '#666666',
                fontSize: 18
              }
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#DCE2E8'
              }
            },
            splitLine: {
              show: false
            }
          }
        ],
        series: [
          {
            name: this.lenged[0],
            type: 'line',
            showAllSymbol: true,
            symbol: 'circle',
            symbolSize: 10,
            lineStyle: {
              normal: {
                color: this.colorList[0]
              }
            },
            // label: {
            //   show: false,
            //   position: 'top',
            //   textStyle: {
            //     color: '#A582EA'
            //   }
            // },
            itemStyle: {
              color: '#fff',
              borderColor: this.colorList[0],
              borderWidth: 2
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(0,0,0,0)'
                    },
                    {
                      offset: 1,
                      color: 'rgba(0,0,0,0)'
                    }
                  ],
                  false
                )
              }
            },
            data: this.chartData.yData[0] // data.values
          },
          {
            name: this.lenged[1],
            type: 'line',
            showAllSymbol: true,
            symbol: 'circle',
            symbolSize: 10,
            lineStyle: {
              normal: {
                color: this.colorList[1]
              }
            },
            // label: {
            //   show: false,
            //   position: 'top',
            //   textStyle: {
            //     color: '#A582EA'
            //   }
            // },
            itemStyle: {
              color: '#fff',
              borderColor: this.colorList[1],
              borderWidth: 2
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(0,0,0,0)'
                    },
                    {
                      offset: 1,
                      color: 'rgba(0,0,0,0)'
                    }
                  ],
                  false
                )
              }
            },
            data: this.chartData.yData[1] // data.values
          },
          {
            name: this.lenged[2],
            type: 'line',
            showAllSymbol: true,
            symbol: 'circle',
            symbolSize: 10,
            lineStyle: {
              normal: {
                color: this.colorList[2]
              }
            },
            // label: {
            //   show: false,
            //   position: 'top',
            //   textStyle: {
            //     color: '#A582EA'
            //   }
            // },
            itemStyle: {
              color: '#fff',
              borderColor: this.colorList[2],
              borderWidth: 2
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(0,0,0,0)'
                    },
                    {
                      offset: 1,
                      color: 'rgba(0,0,0,0)'
                    }
                  ],
                  false
                )
              }
            },
            data: this.chartData.yData[2] // data.values
          },
          {
            name: this.lenged[3],
            type: 'line',
            showAllSymbol: true,
            symbol: 'circle',
            symbolSize: 10,
            lineStyle: {
              normal: {
                color: this.colorList[3]
              }
            },
            // label: {
            //   show: false,
            //   position: 'top',
            //   textStyle: {
            //     color: '#A582EA'
            //   }
            // },
            itemStyle: {
              color: '#fff',
              borderColor: this.colorList[3],
              borderWidth: 2
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: 'rgba(0,0,0,0)'
                    },
                    {
                      offset: 1,
                      color: 'rgba(0,0,0,0)'
                    }
                  ],
                  false
                )
              }
            },
            data: this.chartData.yData[3] // data.values
          }
        ]
      }
      this.chart.setOption(this.scoreOption)
      window.addEventListener('resize', this.onResize)
    },
    onResize() {
      this.chart.resize()
    }
  }
}
</script>
    <style scoped>
.multiBeam {
  width: 100%;
  height: 100%;
}
</style>

